package com.lsyw.intergrate.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 统一返回
 */
@Data
public class Result {
    public static int SUCCESS = ErrorCode.C0.getErrcode();
    public static int ERROR = ErrorCode.C1.getErrcode();

    /**
     * 代码 1 成功 0 失败
     */
    @ApiModelProperty(value = "代码")
    int code;

    /**
     * 数据
     */
    @ApiModelProperty(value = "数据")
    Object data;

    /**
     * 消息
     */
    @ApiModelProperty(value = "消息")
    String message;

    public Result(int code, Object data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    /**
     * 接口增加返回值
     *
     * @param insert 状态
     * @param data   返回数据
     * @return
     */
    public static Result addResult(Integer insert, Object data) {
        if (insert > 0) {
            return new Result(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new Result(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }


    /**
     * 接口删除返回值
     *
     * @param delete 状态
     * @param data   返回数据
     * @return
     */
    public static Result deleteResult(Integer delete, Object data) {
        if (delete > 0) {
            return new Result(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new Result(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }

    /**
     * 接口修改返回值
     *
     * @param update 状态
     * @param data   返回数据
     * @return
     */
    public static Result updateResult(Integer update, Object data) {
        if (update > 0) {
            return new Result(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new Result(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }

    /**
     * 接口查询返回值
     *
     * @param data 返回数据
     * @return
     */
    public static Result searchResult(Object data) {
        if (data != null) {
            return new Result(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new Result(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }


    /**
     * 成功返回
     *
     * @param data 返回数据
     * @return
     */
    public static Result successResult(Object data) {
        return new Result(SUCCESS, data, ErrorCode.C0.getErrmsg());

    }

    /**
     * 失败返回
     *
     * @param data 返回数据
     * @return
     */
    public static Result errorResult(Object data) {
        return new Result(ERROR, data, data.toString());

    }


    /**
     * 失败返回
     *
     * @param data 返回数据
     * @return
     */
    public static Result errorResult(Object data, String message) {
        return new Result(ERROR, data, message);
    }

}
